package net.danlew.android.joda.sample.slice;

import net.danlew.android.joda.sample.DateUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;

import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import org.joda.time.Duration;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class sampleFormatDurationslice extends AbilitySlice {
    private DirectionalLayout directionalLayout = new DirectionalLayout(this);
    private DirectionalLayout.LayoutConfig layoutConfigD = new DirectionalLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT,
            ComponentContainer.LayoutConfig.MATCH_CONTENT);
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        directionalLayout.setWidth(ComponentContainer.LayoutConfig.MATCH_PARENT);
        directionalLayout.setHeight(ComponentContainer.LayoutConfig.MATCH_PARENT);
//            scrollView.setOrientation(Component.VERTICAL);
        directionalLayout.setPadding(32, 32, 80, 80);

        ShapeElement element = new ShapeElement();

        element.setRgbColor(new RgbColor(255, 255, 255));
        directionalLayout.setBackground(element);

        Text welcome = new Text(this);

        welcome.setLayoutConfig(layoutConfigD);
        welcome.setText("sampleFormatDuration测试结果：");
        welcome.setTextSize(85);
        welcome.setTextColor(Color.RED);
        welcome.setMultipleLine(true);
        directionalLayout.addComponent(welcome);
        try {
            sampleFormatDuration();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        } catch (WrongTypeException e) {
            e.printStackTrace();
        }
        super.setUIContent(directionalLayout);
    }
    private void sampleFormatDuration() throws IOException, NotExistException, WrongTypeException {
        List<String> text = new ArrayList<String>();
        text.add("Seconds: " + DateUtils.formatDuration(this, Duration.standardSeconds(25)));
        text.add("Minutes: " + DateUtils.formatDuration(this, Duration.standardMinutes(5)));
        text.add("Hours: " + DateUtils.formatDuration(this, Duration.standardHours(3)));
        addSample("DateUtils.formatDuration()", text);
    }
    private void addSample(CharSequence title, Iterable<String> text) {
        addSample(title, join("\n",text));
    }
    public static String join( CharSequence delimiter,  Iterable tokens) {
        final Iterator<?> it = tokens.iterator();
        if (!it.hasNext()) {
            return "";
        }
        final StringBuilder sb = new StringBuilder();
        sb.append(it.next());
        while (it.hasNext()) {
            sb.append(delimiter);
            sb.append(it.next());
        }
        return sb.toString();
    }

    private void addSample(CharSequence title, CharSequence text) {

//        ((Text) view.findViewById(R.id.title)).setText(title);
//        ((Text) view.findViewById(R.id.text)).setText(text);
        Text titleText = new Text(this);
//            layoutConfig.gravity = LayoutAlignment.CENTER_HORIZONTAL;
        titleText.setLayoutConfig(layoutConfigD);
        titleText.setText((String) title);
        titleText.setTextColor(Color.BLUE);
        titleText.setTextSize(75);
        titleText.setMultipleLine(true);
        Text textT = new Text(this);
//            layoutConfig.gravity = LayoutAlignment.CENTER_HORIZONTAL;
        textT.setLayoutConfig(layoutConfigD);
        textT.setText((String) text);
        textT.setTextSize(70);
        textT.setMultipleLine(true);
        directionalLayout.addComponent(titleText);
        directionalLayout.addComponent(textT);
    }



}
